package cn.hy.preparelesson.module.business;

import cn.hy.preparelesson.common.entity.PlComment;
import cn.hy.preparelesson.common.params.PageParams;
import cn.hy.preparelesson.common.permission.RequirePermissions;
import cn.hy.preparelesson.common.service.PlCommentService;
import cn.hy.preparelesson.common.utils.UserUtils;
import cn.hy.preparelesson.common.utils.ValidatorUtils;
import cn.hy.preparelesson.common.vo.PageInfo;
import cn.hy.preparelesson.common.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 评论管理Controller
 */

@RequestMapping("/comment")
@RestController
public class PlCommentController {

    @Autowired
    private PlCommentService plCommentService;

    /**
     * 保存 修改 评论
     * @param schoolId
     * @param plComment
     * @return
     */
    @RequirePermissions("pl:comment:operating")
    @RequestMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestParam("schoolToken")String schoolId, PlComment plComment) {
        ValidatorUtils.validateEntity(plComment);
        if (StringUtils.isBlank(plComment.getId())) {
            plCommentService.save(schoolId, UserUtils.getUserId(), plComment);
        } else {
            plCommentService.update(schoolId, UserUtils.getUserId(), plComment);
        }
        return Result.success(null);
    }

    /**
     * 删除评论
     * @param schoolId
     * @param id
     * @return
     */
    @RequirePermissions("pl:comment:operating")
    @RequestMapping("/delete")
    public Result delete(@RequestParam("schoolToken")String schoolId, String id) {
        plCommentService.softDeleteById(schoolId, UserUtils.getUserId(), id);
        return Result.success(null);
    }

    /**
     * 分页或许评论信息
     * @param schoolId
     * @param pageParams
     * @param lessonId
     * @return
     */
    @RequestMapping("/list")
    public Result commentPageList(@RequestParam("schoolToken")String schoolId, PageParams pageParams, String lessonId) throws TException {
         PageInfo<PlComment> commentPageInfo =  plCommentService.findValidPageBy(schoolId, pageParams, lessonId);
        return Result.success(commentPageInfo);
    }
}
